package com.zzy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzy.entity.Message;
import com.zzy.entity.User;
import com.zzy.entity.UserRelation;
import com.zzy.mapper.MessageMapper;
import com.zzy.mapper.UserRelationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class UserRelationController {
    @Autowired
    private UserRelationMapper userRelationMapper;

    @Autowired
    private MessageMapper messageMapper;

    @GetMapping("/getMessByFId/{fId}")
    public List<Message> getMessByFId(@PathVariable("fId") Integer fId, HttpServletRequest request){
        //todo 获取用户id 好友id 得到messageid  查询所有的聊天记录 返回渲染
        System.out.println(fId);
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");

        QueryWrapper<UserRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("u_id",user.getUId());
        wrapper.eq("f_id",fId);
        UserRelation userRelation = userRelationMapper.selectOne(wrapper);

        QueryWrapper<Message> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("m_id",userRelation.getMId());
        List<Message> messages = messageMapper.selectList(wrapper1);

        return messages;

    }
}
